﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace HW_g1
{
    class Program
    {
        static async Task<List<string>> Search(string keyword)
        {
            var total_url = new List<string>();
            var searchEngines = new string[] { "https://www.baidu.com/s?wd=", "https://www.bing.com/search?q=" };

            foreach (var searchEngine in searchEngines)
            {
                var httpClient = new HttpClient();
                var response = await httpClient.GetAsync(searchEngine + keyword);

                if (response.IsSuccessStatusCode)
                {
                    var html = await response.Content.ReadAsStringAsync();
                    var pattern = "<a.*?href=\"(?<url>.*?)\".*?>(?<text>.*?)</a>";
                    var matches = Regex.Matches(html, pattern);

                    foreach (Match match in matches)
                    {
                        var url = match.Groups["url"].Value;
                        if (url.StartsWith("http"))
                        {
                            total_url.Add(url);
                        }
                    }
                }

                httpClient.Dispose();
            }

            return total_url;
        }
        static async Task<ConcurrentBag<string>> SearchPhoneNumbers(List<string> total_url)
        {
            var phoneNumbers = new ConcurrentBag<string>();
            var concurrency = new SemaphoreSlim(8); // 控制并发数为8

            var httpClient = new HttpClient();

            var tasks = new List<Task>();

            foreach (var url in total_url)
            {
                await concurrency.WaitAsync();

                tasks.Add(Task.Run(async () =>
                {
                    try
                    {
                        var response = await httpClient.GetAsync(url);

                        if (response.IsSuccessStatusCode)
                        {
                            var html = await response.Content.ReadAsStringAsync();
                            var pattern = @"((\d{3}-\d{8}|\d{4}-\d{7})|\d{11})";
                            var matches = Regex.Matches(html, pattern);

                            foreach (Match match in matches)
                            {
                                phoneNumbers.Add($"{match.Value} - {url}");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"爬取网页{url}时出现异常：{ex.Message}");
                    }
                    finally
                    {
                        concurrency.Release();
                    }
                }));
            }

            await Task.WhenAll(tasks);

            httpClient.Dispose();

            return phoneNumbers;
        }    
        static async Task Main(string[] args)
        {
            Console.WriteLine("请输入关键字：");
            var keyword = Console.ReadLine();

            Console.WriteLine($"开始搜索，请稍等...");

            var total_url = await Search(keyword);
            Console.WriteLine($"搜索结果如下：");

            foreach (var url in total_url)
            {
                Console.WriteLine(url);
            }

            Console.WriteLine($"正在爬取电话号码，请稍等...");

            var phoneNumbers = await SearchPhoneNumbers(total_url);
            Console.WriteLine($"找到以下电话号码：");

            foreach (var phoneNumber in phoneNumbers)
            {
                Console.WriteLine(phoneNumber);
            }

            Console.WriteLine($"请按任意键继续. . .");
            Console.ReadKey();
        }

        

    }
}
